<?php
include 'redirectAdmin.php';
$action = "manage";

if (isset( $_GET["action"] ) ){
	$action = $_GET ["action"];
}


?>

<table class="manage-promotion">
	<tr>
		<td class="left-menu">
			<div><a href="bookmarks.php?page=managePromotion&action=manage">Zarzadzaj promocjami</a></div><br/>
			<div><a href="bookmarks.php?page=managePromotion&action=find">Odszukaj</a></div>		
		</td>
	
		<td>
			<?php 
				if($action=="manage"){
					
				
			$promotion = new Promotion();
			$request = "add";
			if(isset($_GET['pid']) && VariableUtils::isNumber($_GET['pid'])){
					$promotion = PromotionUtils::get($_GET['pid']);
					$request = "modify";
			}else if(isset($_GET['request'])){
				
				
				$promotion->setName(ParamUtils::get("name"));
				$promotion->setDateFrom(ParamUtils::get("dateFrom"));
				$promotion->setDateTo(ParamUtils::get("dateTo"));
				$promotion->setValue(ParamUtils::get("value"));
				$promotion->setDesc(ParamUtils::get("desc"));
				
				
			
				print_r($_POST);
			
			if("add" == $_GET['request']){
				
			PromotionUtils::add($promotion);
				}else{
					$promotion->setId(ParamUtils::get("id"));
					PromotionUtils::modify($promotion);
				}
				$request = "modify";
			}
			?> 
			<div style = " margin-left: 20px;">
			<form action="bookmarks.php?page=managePromotion&action=manage<?php echo "&request=" .$request;?>" method="post" id ="managePromotionForm">
			
									
					<div>Nazwa</div>
					<input type="text" name="name" value="<?php echo $promotion->getName();?>" />
					<div>Data od</div>	
						<input type="text" value="<?php echo ParamUtils::getString("dateFrom") != null ? ParamUtils::getString("dateFrom") : $promotion->getDateFrom();?>" readonly="readonly" name="dateFrom" id="dateFrom"/>
						<a id="df" name="df" onclick="cal.select(document.forms['managePromotionForm'].dateFrom,'df','y-M-d'); return false;" href="#">
							<img src="images/cal.gif"/>
						</a>
						<a onclick="jQuery('#dateFrom').attr('value',''); return false;">x</a>
					<div>Data do</div>	
						<input type="text" value="<?php echo ParamUtils::getString("dateTo") != null ? ParamUtils::getString("dateTo") : $promotion->getDateTo();?>"  readonly="readonly" name="dateTo" id="dateTo"/>
						<a id="dt" name="dt" onclick="cal.select(document.forms['managePromotionForm'].dateTo,'dt','y-M-d'); return false;" href="#">
							<img src="images/cal.gif"/>
						</a>
						<a onclick="jQuery('#dateTo').attr('value',''); return false;">x</a>
					<div>Wartość</div>
					<input type="text" name="value" value="<?php echo $promotion->getValue();?>" />
					<div>Opis</div>
					<input type="text" name="desc" value="<?php echo $promotion->getDesc();?>" />
			
			<input type="hidden" name="id" value="<?php echo $promotion->getId();?>" /><br/><br/>
			<input type="submit" value="Zapisz" />
			</form>
			</div>
			
	<?php 		
	}else{ //szukanie
			?>
			<?php $pid=null;
			if("remove" == ParamUtils::get("subaction") && ($pid = ParamUtils::get("pid"))!=null){
			PromotionUtils::removeById($pid);
			}
			
			?>	<div style = " margin-left: 20px;">
				<div><b>Wyszukanie promocji</b></div>
				<div>
					<form action="bookmarks.php?page=managePromotion&action=find" method="post" id ="managePromotionFind">
					<div>Nazwa</div>
					<input type="text" name="name" value="<?php echo ParamUtils::getString("name"); ?>"/>
					
					<div>
								<div>Data od</div>
								<input type="text" value="<?php echo ParamUtils::getString("dateFrom");?>" readonly="readonly" name="dateFrom" id="dateFrom"/>
								<a id="df" name="df" onclick="cal.select(document.forms['managePromotionFind'].dateFrom,'df','y-M-d'); return false;" href="#">
									<img src="images/cal.gif"/>
								</a>
								<a onclick="jQuery('#dateFrom').attr('value',''); return false;">x</a>
					</div>
					
					<div>
								<div>Data do</div>
								<input type="text" value="<?php echo ParamUtils::getString("dateTo");?>"  readonly="readonly" name="dateTo" id="dateTo"/>
								<a id="dt" name="dt" onclick="cal.select(document.forms['managePromotionFind'].dateTo,'dt','y-M-d'); return false;" href="#">
									<img src="images/cal.gif"/>
								</a>
								<a onclick="jQuery('#dateTo').attr('value',''); return false;">x</a>
					</div>
					
					<div>Id użytkownika</div><input type="text" name="uid" value="<?php echo ParamUtils::get("uid")?>"/>
					
					<input type="hidden" value="true" name="search" /><br/><br/>
					<input type="submit" value="Szukaj" /> 
					</form>
				
				</div>		
				</div>	
					
				<?php
					$result = null;
					if(isset($_POST['search'])){

						$controls = new PromotionSearchControls();
						$controls->setName(ParamUtils::get("name"));
						$controls->setDateFrom(ParamUtils::get("dateFrom"));
						$controls->setDateTo(ParamUtils::get("dateTo"));
						$controls->setUid(ParamUtils::get("uid"));
						print_r($_POST);
						$result = PromotionUtils::find($controls);
						
						if(is_array($result) && sizeof($result)>0){
							?> 
							
							<div style = " margin-left: 20px;">
							<table>
							<tr>
								<th>Id</th>
								<th>Nazwa</th>					
								<th>Data od</th>
								<th>Data do</th>
								<th>Cena</th>
								<th>Opis</th>
							</tr>
							<?php foreach($result as $promotion):?>
							<tr>
								<td><?php echo $promotion->getId();?></td>
								<td><?php echo $promotion->getName();?></td>
								<td><?php echo $promotion->getDateFrom();?></td>
								<td><?php echo $promotion->getDateTo();?></td>
								<td><?php echo $promotion->getValue();?></td>
								<td><?php echo $promotion->getDesc();?></td>
								<td><a href ="bookmarks.php?page=managePromotion&action=manage&pid=<?php echo $promotion->getId();?>">Zarządzaj</a></td>
								<td><a href ="bookmarks.php?page=managePromotion&action=find&pid=<?php echo $promotion->getId();?>&subaction=remove">Usun</a></td>
							</tr>
							<?php endforeach; ?>
							
							</table>
							</div>
						<?php 	
						}
					}
				
				}?> 	
		
		</td>	
	
	</tr>

</table>

